import { boot } from "quasar/wrappers"
import { LoadingBar, Loading, QSpinnerGears } from "quasar"
import { router } from "@/router"

// import { useUserStore } from 'src/stores/user'
const whiteList = ["Login", "Init"]
import { useUserStore } from "@/stores/use_user_store"
// import { useRouterStore } from '@/stores/use_router_store'

LoadingBar.setDefaults({
    color: "red",
    size: "4px",
    position: "top",
})

function startLoading() {
    Loading.show({
        spinner: QSpinnerGears,
        message: "系统加载中……",
    })
    LoadingBar.start()
}

function stopLoading() {
    Loading.hide()
    LoadingBar.stop()
}

router.beforeEach(async (to, from) => {
    const user = useUserStore()
    startLoading()
    const token = user.token
    // console.log(`token 👉`, token)
    // 在白名单中的判断情况
    if (whiteList.indexOf(to.name) > -1) {
        // next()
        // stopLoading()
        return true
    } else {
        // 不在白名单中并且已经登录的时候
        // console.log(`不在白名单中并且已经登录的时候 ing👇`)
        if (token) {
            // const res = await user.GetUserInfo()
            // // console.log(`GetUserInfo res 👉`,res)
            // if (res.code != 0) {
            //     console.log(`有token缓存，但是获取用户信息失败 👉`, res)
            // }
            // stopLoading()
            return true
        }
        // 不在白名单中并且未登录的时候
        if (!token) {
            console.log(`不在白名单中并且未登录的时候 ing👇`)
            stopLoading()
            return {
                name: "Login",
                query: {
                    redirect: document.location.hash,
                },
            }
        }
    }
})

router.afterEach(() => {
    // console.log(`afterEach ing👇`)
    stopLoading()
})

router.onError(() => {
    stopLoading()
})

stopLoading()
